//
//  ZOEDatePickerView.m
//  HealthyCity
//
//  Created by xxb on 2017/8/17.
//  Copyright © 2017年 智业互联网络科技有限公司 艾嘉健康. All rights reserved.
//

#import "ZOEDatePickerView.h"
#import "ZOEPickerHeaderView.h"

#define ZOEDatePickerViewRowHeight 35 //每一行的高度
#define DefaultTextColor [UIColor colorWithRed:146/255.0 green:146/255.0 blue:146/255.0 alpha:1] //日期默认颜色
#define SelectedTextColor [UIColor colorWithRed:34/255.0 green:34/255.0 blue:34/255.0 alpha:1] //日期选中颜色

#define Year @"year"
#define Month @"month"
#define Day @"day"
#define Hour @"hour"
#define Minute @"minute"
#define Noon @"noon" //上午/下午
#define Year1 @"year1"
#define Month1 @"month1"
#define PickerDivisionTip @"Division" //”至“

#define MaxYear 10000 //最大的年份

@interface ZOEDatePickerView()<UIPickerViewDataSource, UIPickerViewDelegate, ZOEPickerHeaderViewDelegate>

//模式
@property (nonatomic, assign) DatePickerMode datePickerMode;
@property (nonatomic, assign) ShowMode showMode;

//页面元素
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIPickerView *datePickerView;
@property (nonatomic, strong) ZOEPickerHeaderView *headerView;

//日期控制相关
@property (nonatomic, strong) NSCalendar *calendar;
@property (nonatomic, strong) NSDateComponents *todayComponents;
@property (nonatomic, strong) NSDate *displayDate;//当前要显示的日期。
@property (nonatomic, strong) NSDateComponents *displayComponents;
@property (nonatomic, strong) NSDate *displayDate1;//当前要显示的日期。
@property (nonatomic, strong) NSDateComponents *displayComponents1;
@property (nonatomic, strong) NSDateComponents *earliestDateComponents;
@property (nonatomic, strong) NSDateComponents *latestDateComponents;

//用于当月、天显示的已经超过当前值的时候的展示
@property (nonatomic, assign) BOOL flag;

@end

@implementation ZOEDatePickerView

- (instancetype)initWithMode:(ShowMode)showMode andDatePickerMode:(DatePickerMode)datePickerMode {
    return [self initWithFrame:CGRectZero andMode:showMode andDatePickerMode:datePickerMode andDefualtDate:nil];
}

- (instancetype)initWithFrame:(CGRect)frame andMode:(ShowMode)showMode andDatePickerMode:(DatePickerMode)datePickerMode {
    return [self initWithFrame:frame andMode:showMode andDatePickerMode:datePickerMode andDefualtDate:nil];
}

- (instancetype)initWithMode:(ShowMode)showMode andDatePickerMode:(DatePickerMode)datePickerMode andDefualtDate:(NSDate*)defaultDate {
    return [self initWithFrame:CGRectZero andMode:showMode andDatePickerMode:datePickerMode andDefualtDate:defaultDate];
}

- (instancetype)initWithFrame:(CGRect)frame andMode:(ShowMode)showMode andDatePickerMode:(DatePickerMode)datePickerMode andDefualtDate:(NSDate*)defaultDate {
    self = [super init];
    if(self){
        _datePickerMode = datePickerMode;
        _showMode = showMode;
        
        [self setUI:frame];
        
        //年月日
        _calendar = [NSCalendar currentCalendar];
        _todayComponents = [_calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:[NSDate date]];
        [self setDefaultDate:defaultDate];
    }
    return self;
}

#pragma mark - delegate

#pragma mark -UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    if(_datePickerMode == DatePickerModeYear){
        return 1;
    }else if(_datePickerMode == DatePickerModeYearMonth){
        return 2;
    }else if(_datePickerMode == DatePickerModeYearMonthDay){
        return 3;//年、月、日
    }else if(_datePickerMode == DatePickerModeYearMonthDayHour){
        return 4;
    }else if(_datePickerMode == DatePickerModeYearMonthDayHourMinute){
        return 5;
    }else if(_datePickerMode == DatePickerModeHourMinute24){
        return 2;//时分
    }else if(_datePickerMode == DatePickerModeTwoYearMonth){
        return 5;
    }
    return 0;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    __block NSInteger rows;
    [self blockForComponent:component year:^{
        if(self.earliestDate || self.latestDate){
            //有设置极限时间
            NSInteger from = 1;
            NSInteger to = MaxYear;
            if(self.earliestDate){
                from = self.earliestDateComponents.year;
            }
            if(self.latestDate){
                to = self.latestDateComponents.year;
            }
            rows = to - from + 1;
        }else{
            rows = MaxYear;
        }
    } month:^{
        if(self.earliestDate &&
           self.displayComponents.year == self.earliestDateComponents.year){
            rows = 12 - self.earliestDateComponents.month + 1;
        }else if(self.latestDate &&
                 self.displayComponents.year == self.latestDateComponents.year){
            rows = self.latestDateComponents.month;
        }else{
            rows = 12;
        }
    } day:^{
        if(self.earliestDate &&
           self.displayComponents.year == self.earliestDateComponents.year &&
           self.displayComponents.month == self.earliestDateComponents.month){
            NSRange dayRange = [self.calendar rangeOfUnit:NSCalendarUnitDay
                                                   inUnit:NSCalendarUnitMonth
                                                  forDate:self.earliestDate];
            rows = dayRange.length - self.earliestDateComponents.day + 1;
        }else if(self.latestDate &&
                 self.displayComponents.year == self.latestDateComponents.year &&
                 self.displayComponents.month == self.latestDateComponents.month){
            rows = self.latestDateComponents.day;
        }else{
            NSRange dayRange = [self.calendar rangeOfUnit:NSCalendarUnitDay
                                                   inUnit:NSCalendarUnitMonth
                                                  forDate:self.displayDate];
            rows = dayRange.length;
        }
    } hour:^{
        //时从0-23/0-11
        if(self.earliestDate &&
           self.displayComponents.year == self.earliestDateComponents.year &&
           self.displayComponents.month == self.earliestDateComponents.month &&
           self.displayComponents.day == self.earliestDateComponents.day){
            rows = 24 - self.earliestDateComponents.hour;
        }else if(self.latestDate &&
                 self.displayComponents.year == self.latestDateComponents.year &&
                 self.displayComponents.month == self.latestDateComponents.month &&
                 self.displayComponents.day == self.latestDateComponents.day){
            rows = self.latestDateComponents.hour + 1;
        }else{
            rows = 24;
        }
    } minute:^{
        //分钟数从0-59
        if(self.earliestDate &&
           self.displayComponents.year == self.earliestDateComponents.year &&
           self.displayComponents.month == self.earliestDateComponents.month &&
           self.displayComponents.day == self.earliestDateComponents.day &&
           self.displayComponents.hour == self.earliestDateComponents.hour){
            rows = 60 - self.earliestDateComponents.minute;
        }else if(self.latestDate &&
                 self.displayComponents.year == self.latestDateComponents.year &&
                 self.displayComponents.month == self.latestDateComponents.month &&
                 self.displayComponents.day == self.latestDateComponents.day &&
                 self.displayComponents.hour == self.latestDateComponents.hour){
            rows = self.latestDateComponents.minute + 1;
        }else{
            rows = 60;
        }
    } noon:nil year1:^{
        if(self.earliestDate || self.latestDate){
            //有设置极限时间
            NSInteger from = 1;
            NSInteger to = MaxYear;
            if(self.earliestDate){
                from = self.earliestDateComponents.year;
            }
            if(self.latestDate){
                to = self.latestDateComponents.year;
            }
            rows = to - from + 1;
        }else{
            rows = MaxYear;
        }
    } month1:^{
        if(self.earliestDate &&
           self.displayComponents1.year == self.earliestDateComponents.year){
            rows = 12 - self.earliestDateComponents.month + 1;
        }else if(self.latestDate &&
                 self.displayComponents1.year == self.latestDateComponents.year){
            rows = self.latestDateComponents.month;
        }else{
            rows = 12;
        }
    } divisionBlock:^{
        rows = 1;
    }];
    return rows;
}


#pragma mark -UIPickerViewDelegate

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return ZOEDatePickerViewRowHeight;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view {
    UILabel *dateLabel = (UILabel *)view;
    if (!dateLabel) {
        dateLabel = [[UILabel alloc] init];
        [dateLabel setFont:[UIFont systemFontOfSize:18]];
        [dateLabel setTextColor:DefaultTextColor];
        [dateLabel setBackgroundColor:[UIColor clearColor]];
        dateLabel.textAlignment = NSTextAlignmentCenter;
    }
    __block BOOL select = NO;
    [self blockForComponent:component year:^{
        NSString *text;
        NSInteger year;
        if(self.earliestDate){
            year = self.earliestDateComponents.year + row;
            text = [NSString stringWithFormat:@"%ld", year];
        }else{
            year = (long)row + 1;
            text = [NSString stringWithFormat:@"%ld", year];
        }
        if(!self.unitDisabled){
            text = [text stringByAppendingString:@"年"];
        }
        dateLabel.text = text;
        if(year == self.displayComponents.year){
            select = YES;
        }
    } month:^{
        NSString *text;
        NSInteger month;
        if(self.earliestDate &&
           self.earliestDateComponents.year == self.displayComponents.year){
            month = self.earliestDateComponents.month + row;
            text = [NSString stringWithFormat:@"%ld", month];
        }else{
            month = (long)row + 1;
            text = [NSString stringWithFormat:@"%ld", month];
        }
        if(!self.unitDisabled){
            text = [text stringByAppendingString:@"月"];
        }
        dateLabel.text = text;
        if(month == self.displayComponents.month){
            select = YES;
        }
    } day:^{
        NSString *text;
        NSInteger day;
        if(self.earliestDate &&
           self.earliestDateComponents.year == self.displayComponents.year &&
           self.earliestDateComponents.month == self.displayComponents.month){
            day = self.earliestDateComponents.day + row;
            text = [NSString stringWithFormat:@"%ld", day];
        }else{
            day = (long)row + 1;
            text = [NSString stringWithFormat:@"%ld", day];
        }
        if(!self.unitDisabled){
            text = [text stringByAppendingString:@"日"];
        }
        dateLabel.text = text;
        if(day == self.displayComponents.day){
            select = YES;
        }
    } hour:^{
        NSInteger hour = 0;
        if(self.earliestDate &&
           self.earliestDateComponents.year == self.displayComponents.year &&
           self.earliestDateComponents.month == self.displayComponents.month &&
           self.earliestDateComponents.day == self.displayComponents.day){
            hour = self.earliestDateComponents.hour + row;
        }else{
            hour = row;
        }
        NSString *text;
        text = [NSString stringWithFormat:@"%ld", (long)hour];
        if(!self.unitDisabled){
            text = [text stringByAppendingString:@"时"];
        }
        dateLabel.text = text;
        if(hour == self.displayComponents.hour){
            select = YES;
        }
    } minute:^{
        NSString *text;
        NSInteger minute;
        if(self.earliestDate &&
           self.earliestDateComponents.year == self.displayComponents.year &&
           self.earliestDateComponents.month == self.displayComponents.month &&
           self.earliestDateComponents.day == self.displayComponents.day &&
           self.earliestDateComponents.hour == self.displayComponents.hour){
            minute = self.earliestDateComponents.minute + row;
            text = [NSString stringWithFormat:@"%ld", minute];
        }else{
            minute = (long)row;
            text = [NSString stringWithFormat:@"%ld", minute];
        }
        if(!self.unitDisabled){
            text = [text stringByAppendingString:@"分"];
        }
        dateLabel.text = text;
        if(minute == self.displayComponents.minute){
            select = YES;
        }
    } noon:nil year1:^{
        NSString *text;
        NSInteger year;
        if(self.earliestDate){
            year = self.earliestDateComponents.year + row;
            text = [NSString stringWithFormat:@"%ld", year];
        }else{
            year = (long)row + 1;
            text = [NSString stringWithFormat:@"%ld", year];
        }
        if(!self.unitDisabled){
            text = [text stringByAppendingString:@"年"];
        }
        dateLabel.text = text;
        if(year == self.displayComponents1.year){
            select = YES;
        }
    } month1:^{
        NSString *text;
        NSInteger month;
        if(self.earliestDate &&
           self.earliestDateComponents.year == self.displayComponents1.year){
            month = self.earliestDateComponents.month + row;
            text = [NSString stringWithFormat:@"%ld", month];
        }else{
            month = (long)row + 1;
            text = [NSString stringWithFormat:@"%ld", month];
        }
        if(!self.unitDisabled){
            text = [text stringByAppendingString:@"月"];
        }
        dateLabel.text = text;
        if(month == self.displayComponents1.month){
            select = YES;
        }
    } divisionBlock:^{
        dateLabel.text = @"至";
        select = YES;
    }];
    if(select){
        dateLabel.textColor = SelectedTextColor;
    }else{
        dateLabel.textColor = DefaultTextColor;
    }
    return dateLabel;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    //设置选中日期
    [self blockForComponent:component year:^{
        if(self.earliestDate){
            self.displayComponents.year = (NSUInteger)(row + self.earliestDateComponents.year);
        }else{
            self.displayComponents.year = (NSUInteger)(row + 1);
        }
    } month:^{
        NSUInteger correctMonth;
        if(self.earliestDate &&
           self.displayComponents.year == self.earliestDateComponents.year){
            correctMonth = (NSUInteger)(row + self.earliestDateComponents.month);
        }else{
            correctMonth = (NSUInteger)(row + 1);
        }
        self.displayComponents.month = correctMonth;
        NSDate *date = [self.calendar dateFromComponents:self.displayComponents];
        NSDateComponents *tempComponents = [self.calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];
        if(correctMonth != tempComponents.month){
            //因为月份的天数不同导致系统自动改变了月份的情况
            tempComponents.month = correctMonth;
            tempComponents.day = 1;
            date = [self.calendar dateFromComponents:tempComponents];
            NSRange dayRange = [self.calendar rangeOfUnit:NSCalendarUnitDay
                                                   inUnit:NSCalendarUnitMonth
                                                  forDate:date];
            self.displayComponents.month = correctMonth;
            self.displayComponents.day = dayRange.length;
        }
    } day:^{
        if(self.earliestDate &&
           self.displayComponents.year == self.earliestDateComponents.year &&
           self.displayComponents.month == self.earliestDateComponents.month){
            self.displayComponents.day = (NSUInteger)(row + self.earliestDateComponents.day);
        }else{
            self.displayComponents.day = (NSUInteger)(row + 1);
        }
    } hour:^{
        if(self.earliestDate &&
           self.displayComponents.year == self.earliestDateComponents.year &&
           self.displayComponents.month == self.earliestDateComponents.month &&
           self.displayComponents.day == self.earliestDateComponents.day){
            self.displayComponents.hour = (NSUInteger)(row + self.earliestDateComponents.hour);
        }else{
            self.displayComponents.hour = (NSUInteger)row;
        }
    } minute:^{
        if(self.earliestDate &&
           self.displayComponents.year == self.earliestDateComponents.year &&
           self.displayComponents.month == self.earliestDateComponents.month &&
           self.displayComponents.day == self.earliestDateComponents.day &&
           self.displayComponents.hour == self.earliestDateComponents.hour){
            self.displayComponents.minute = (NSUInteger)(row + self.earliestDateComponents.minute);
        }else{
            self.displayComponents.minute = (NSUInteger)row;
        }
    } noon:nil year1:^{
        if(self.earliestDate){
            self.displayComponents1.year = (NSUInteger)(row + self.earliestDateComponents.year);
        }else{
            self.displayComponents1.year = (NSUInteger)(row + 1);
        }
    } month1:^{
        NSUInteger correctMonth;
        if(self.earliestDate &&
           self.displayComponents1.year == self.earliestDateComponents.year){
            correctMonth = (NSUInteger)(row + self.earliestDateComponents.month);
        }else{
            correctMonth = (NSUInteger)(row + 1);
        }
        self.displayComponents1.month = correctMonth;
        NSDate *date = [self.calendar dateFromComponents:self.displayComponents1];
        NSDateComponents *tempComponents = [self.calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];
        if(correctMonth != tempComponents.month){
            //因为月份的天数不同导致系统自动改变了月份的情况
            tempComponents.month = correctMonth;
            tempComponents.day = 1;
            date = [self.calendar dateFromComponents:tempComponents];
            NSRange dayRange = [self.calendar rangeOfUnit:NSCalendarUnitDay
                                                   inUnit:NSCalendarUnitMonth
                                                  forDate:date];
            self.displayComponents1.month = correctMonth;
            self.displayComponents1.day = dayRange.length;
        }
    } divisionBlock:nil];
    
    self.displayDate = [self.calendar dateFromComponents:self.displayComponents];
    self.displayDate1 = [self.calendar dateFromComponents:self.displayComponents1];
    //    [pickerView reloadAllComponents];
    
    [self overLimitDayUpdate];
    //在view模式下才调用
    if(_showMode == ShowInView){
        if([self.delegate respondsToSelector:@selector(selectedWithDate:)]){
            [self.delegate selectedWithDate:self.displayDate];
        }
    }
}

#pragma mark -ZOEPickerHeaderViewDelegate

- (void)cancel {
    [self.bgView removeFromSuperview];
    [self removeFromSuperview];
    if([self.delegate respondsToSelector:@selector(cancelSelectAtDatePickerView:)]){
        [self.delegate cancelSelectAtDatePickerView:self];
    }
}

- (void)confirm {
    [self.bgView removeFromSuperview];
    [self removeFromSuperview];
    if(_datePickerMode == DatePickerModeTwoYearMonth){
        if([self.delegate respondsToSelector:@selector(datePickerView:confirmWithDate:date1:)]){
            self.displayDate = [self.calendar dateFromComponents:self.displayComponents];
            self.displayDate1 = [self.calendar dateFromComponents:self.displayComponents1];
            [self.delegate datePickerView:self confirmWithDate:self.displayDate date1:self.displayDate1];
        }else if([self.delegate respondsToSelector:@selector(confirmWithDate:date1:)]){
            self.displayDate = [self.calendar dateFromComponents:self.displayComponents];
            self.displayDate1 = [self.calendar dateFromComponents:self.displayComponents1];
            [self.delegate confirmWithDate:self.displayDate date1:self.displayDate1];
        }
    }else{
        if([self.delegate respondsToSelector:@selector(datePickerView:confirmWithDate:)]){
            self.displayDate = [self.calendar dateFromComponents:self.displayComponents];
            [self.delegate datePickerView:self confirmWithDate:self.displayDate];
        }else if([self.delegate respondsToSelector:@selector(confirmWithDate:)]){
            self.displayDate = [self.calendar dateFromComponents:self.displayComponents];
            [self.delegate confirmWithDate:self.displayDate];
        }else if([self.delegate respondsToSelector:@selector(confirmWithDate:string:)]){
            self.displayDate = [self.calendar dateFromComponents:self.displayComponents];
            [self.delegate confirmWithDate:self.displayDate string:[self getPickerViewContent]];
        }else if([self.delegate respondsToSelector:@selector(datePickerView:confirmWithDate:string:)]){
            self.displayDate = [self.calendar dateFromComponents:self.displayComponents];
            [self.delegate datePickerView:self confirmWithDate:self.displayDate string:[self getPickerViewContent]];
        }
    }
}

#pragma mark - Event

- (void)setUI:(CGRect)frame {
    if(_showMode == ShowInView){
        self.frame = frame;
        [self addSubview:self.datePickerView];
    }else if(_showMode == ShowInWindow){
        self.backgroundColor = [UIColor whiteColor];
        self.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 216, [[UIScreen mainScreen] bounds].size.width, 216);
        [self addSubview:self.headerView];
        [self addSubview:self.datePickerView];
        self.datePickerView.frame = CGRectMake(10, CGRectGetMaxY(self.headerView.frame), self.frame.size.width - 20, self.frame.size.height - self.headerView.frame.size.height);
    }
}

- (void)setDefaultDate:(NSDate*)defaultDate {
    if(defaultDate){
        self.displayDate = [defaultDate copy];
        self.displayDate1 = [defaultDate copy];
    }else{
        self.displayDate = [NSDate date];
        self.displayDate1 = [NSDate date];
    }
    self.displayComponents = [self.calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:self.displayDate];
    self.displayComponents1 = [self.calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:self.displayDate1];
}

- (void)setUnitDisabled:(BOOL)showUnit {
    _unitDisabled = showUnit;
    [self.datePickerView reloadAllComponents];
}

- (void)setTitle:(NSString *)title {
    self.headerView.title = title;
}

/**
 设置选中日期后，刷新UI
 */
- (void)updateDisplay {
    //日期符合的话进行刷新
    if(!self.displayDate){
        return;
    }
    if(self.datePickerMode == DatePickerModeTwoYearMonth && !self.displayDate1){
        return;
    }
    if(self.earliestDate && [self.earliestDate timeIntervalSinceDate:self.displayDate] > 0){
        return;
    }
    if(self.latestDate && [self.latestDate timeIntervalSinceDate:self.displayDate] < 0){
        return;
    }
    if(self.datePickerMode == DatePickerModeTwoYearMonth && self.earliestDate && [self.earliestDate timeIntervalSinceDate:self.displayDate1] > 0){
        return;
    }
    if(self.datePickerMode == DatePickerModeTwoYearMonth && self.latestDate && self.displayDate1 && [self.latestDate timeIntervalSinceDate:self.displayDate1] < 0){
        return;
    }
    [self overLimitDayUpdate];
}

-(void)blockForComponent:(NSInteger)component year:(void(^)(void))yearBlock month:(void(^)(void))monthBlock day:(void(^)(void))dayBlock hour:(void(^)(void))hourBlock minute:(void(^)(void))minuteBlock noon:(void(^)(void))noonBlock year1:(void(^)(void))yearBlock1 month1:(void(^)(void))monthBlock1 divisionBlock:(void(^)(void))divisionBlock{
    //年
    if(component == [self getComponentBy:Year]){
        if(yearBlock) yearBlock();
    }
    //月
    if(component == [self getComponentBy:Month]){
        if(monthBlock) monthBlock();
    }
    //日
    if(component == [self getComponentBy:Day]){
        if(dayBlock) dayBlock();
    }
    //时
    if(component == [self getComponentBy:Hour]){
        if(hourBlock) hourBlock();
    }
    //分
    if(component == [self getComponentBy:Minute]){
        if(minuteBlock) minuteBlock();
    }
    //下午/上午
    //    if(component == [self getComponentBy:Noon]){
    //        if(noonBlock) noonBlock();
    //    }
    //年
    if(component == [self getComponentBy:Year1]){
        if(yearBlock1) yearBlock1();
    }
    //月
    if(component == [self getComponentBy:Month1]){
        if(monthBlock1) monthBlock1();
    }
    if(component == [self getComponentBy:PickerDivisionTip]){
        if(divisionBlock) divisionBlock();
    }
}

/**
 合理值设置；刷新component；选中item
 */
-(void)overLimitDayUpdate {
    if([self getComponentBy:Year] != -1){
        [self.datePickerView reloadComponent:[self getComponentBy:Year]];
        if(self.earliestDate){
            [self.datePickerView selectRow:self.displayComponents.year - self.earliestDateComponents.year inComponent:[self getComponentBy:Year] animated:YES];
        }else{
            [self.datePickerView selectRow:self.displayComponents.year - 1 inComponent:[self getComponentBy:Year] animated:YES];
        }
    }
    //月
    if([self getComponentBy:Month] != -1){
        //设值
        if(self.latestDate &&
           self.displayComponents.year >= self.latestDateComponents.year &&
           self.displayComponents.month > self.latestDateComponents.month){
            self.displayComponents.month = self.latestDateComponents.month;
        }else if(self.earliestDate &&
                 self.displayComponents.year <= self.earliestDateComponents.year &&
                 self.displayComponents.month < self.earliestDateComponents.month){
            self.displayComponents.month = self.earliestDateComponents.month;
        }
        //刷新
        [self.datePickerView reloadComponent:[self getComponentBy:Month]];
        //选中
        if(self.earliestDate &&
           self.displayComponents.year <= self.earliestDateComponents.year){
            [self.datePickerView selectRow:self.displayComponents.month - self.earliestDateComponents.month inComponent:[self getComponentBy:Month] animated:NO];
        }else{
            [self.datePickerView selectRow:self.displayComponents.month - 1 inComponent:[self getComponentBy:Month] animated:NO];
        }
    }
    //日
    if([self getComponentBy:Day] != -1){
        //设值
        if(self.latestDate &&
           self.displayComponents.year >= self.latestDateComponents.year &&
           self.displayComponents.month >= self.latestDateComponents.month &&
           self.displayComponents.day > self.latestDateComponents.day){
            self.displayComponents.day = self.latestDateComponents.day;
        }else if(self.earliestDate &&
                 self.displayComponents.year <= self.earliestDateComponents.year &&
                 self.displayComponents.month <= self.earliestDateComponents.month &&
                 self.displayComponents.day < self.earliestDateComponents.day){
            self.displayComponents.day = self.earliestDateComponents.day;
        }
        //刷新
        [self.datePickerView reloadComponent:[self getComponentBy:Day]];
        //选中
        if(self.earliestDate &&
           self.displayComponents.year <= self.earliestDateComponents.year &&
           self.displayComponents.month <= self.earliestDateComponents.month){
            [self.datePickerView selectRow:self.displayComponents.day - self.earliestDateComponents.day inComponent:[self getComponentBy:Day] animated:NO];
        }else{
            [self.datePickerView selectRow:self.displayComponents.day - 1 inComponent:[self getComponentBy:Day] animated:NO];
        }
    }
    //时
    if([self getComponentBy:Hour] != -1){
        //设值
        if(self.latestDate &&
           self.displayComponents.year >= self.latestDateComponents.year &&
           self.displayComponents.month >= self.latestDateComponents.month &&
           self.displayComponents.day >= self.latestDateComponents.day &&
           self.displayComponents.hour > self.latestDateComponents.hour){
            self.displayComponents.hour = self.latestDateComponents.hour;
        }else if(self.earliestDate &&
                 self.displayComponents.year <= self.earliestDateComponents.year &&
                 self.displayComponents.month <= self.earliestDateComponents.month &&
                 self.displayComponents.day <= self.earliestDateComponents.day &&
                 self.displayComponents.hour < self.earliestDateComponents.hour){
            self.displayComponents.hour = self.earliestDateComponents.hour;
        }
        //刷新
        [self.datePickerView reloadComponent:[self getComponentBy:Hour]];
        //选中
        if(self.earliestDate &&
           self.displayComponents.year <= self.earliestDateComponents.year &&
           self.displayComponents.month <= self.earliestDateComponents.month &&
           self.displayComponents.day <= self.earliestDateComponents.day){
            [self.datePickerView selectRow:self.displayComponents.hour - self.earliestDateComponents.hour inComponent:[self getComponentBy:Hour] animated:NO];
        }else{
            [self.datePickerView selectRow:self.displayComponents.hour inComponent:[self getComponentBy:Hour] animated:NO];
        }
    }
    //分
    if([self getComponentBy:Minute] != -1){
        if(self.latestDate &&
           self.displayComponents.year >= self.latestDateComponents.year &&
           self.displayComponents.month >= self.latestDateComponents.month &&
           self.displayComponents.day >= self.latestDateComponents.day &&
           self.displayComponents.hour >= self.latestDateComponents.hour &&
           self.displayComponents.minute > self.latestDateComponents.minute){
            self.displayComponents.minute = self.latestDateComponents.minute;
        }else if(self.earliestDate &&
                 self.displayComponents.year <= self.earliestDateComponents.year &&
                 self.displayComponents.month <= self.earliestDateComponents.month &&
                 self.displayComponents.day <= self.earliestDateComponents.day &&
                 self.displayComponents.hour <= self.earliestDateComponents.hour &&
                 self.displayComponents.minute < self.earliestDateComponents.minute){
            self.displayComponents.minute = self.earliestDateComponents.minute;
        }
        //刷新
        [self.datePickerView reloadComponent:[self getComponentBy:Minute]];
        //选中
        if(self.earliestDate &&
           self.displayComponents.year <= self.earliestDateComponents.year &&
           self.displayComponents.month <= self.earliestDateComponents.month &&
           self.displayComponents.day <= self.earliestDateComponents.day &&
           self.displayComponents.hour <= self.earliestDateComponents.hour){
            [self.datePickerView selectRow:self.displayComponents.minute - self.earliestDateComponents.minute inComponent:[self getComponentBy:Minute] animated:NO];
        }else{
            [self.datePickerView selectRow:self.displayComponents.minute inComponent:[self getComponentBy:Minute] animated:NO];
        }
    }
    
    if([self getComponentBy:Year1] != -1){
        [self.datePickerView reloadComponent:[self getComponentBy:Year1]];
        if(self.earliestDate){
            [self.datePickerView selectRow:self.displayComponents1.year - self.earliestDateComponents.year inComponent:[self getComponentBy:Year1] animated:YES];
        }else{
            [self.datePickerView selectRow:self.displayComponents1.year - 1 inComponent:[self getComponentBy:Year1] animated:YES];
        }
    }
    //月
    if([self getComponentBy:Month1] != -1){
        //设值
        if(self.latestDate &&
           self.displayComponents1.year >= self.latestDateComponents.year &&
           self.displayComponents1.month > self.latestDateComponents.month){
            self.displayComponents1.month = self.latestDateComponents.month;
        }else if(self.earliestDate &&
                 self.displayComponents1.year <= self.earliestDateComponents.year &&
                 self.displayComponents1.month < self.earliestDateComponents.month){
            self.displayComponents1.month = self.earliestDateComponents.month;
        }
        //刷新
        [self.datePickerView reloadComponent:[self getComponentBy:Month1]];
        //选中
        if(self.earliestDate &&
           self.displayComponents1.year <= self.earliestDateComponents.year){
            [self.datePickerView selectRow:self.displayComponents1.month - self.earliestDateComponents.month inComponent:[self getComponentBy:Month1] animated:NO];
        }else{
            [self.datePickerView selectRow:self.displayComponents1.month - 1 inComponent:[self getComponentBy:Month1] animated:NO];
        }
    }
}

/**
 根据type获取Component（类似索引）
 
 @param type type description
 @return return value description
 */
-(NSInteger)getComponentBy:(NSString*)type{
    if([type isEqualToString:Year]){
        if(self.datePickerMode == DatePickerModeYear ||
           self.datePickerMode == DatePickerModeYearMonthDay ||
           self.datePickerMode == DatePickerModeYearMonth ||
           self.datePickerMode == DatePickerModeYearMonthDayHour ||
           self.datePickerMode == DatePickerModeYearMonthDayHourMinute ||
           self.datePickerMode == DatePickerModeTwoYearMonth){
            return 0;
        }
    }
    if([type isEqualToString:Month]){
        if(self.datePickerMode == DatePickerModeYearMonthDay ||
           self.datePickerMode == DatePickerModeYearMonth ||
           self.datePickerMode == DatePickerModeYearMonthDayHour ||
           self.datePickerMode == DatePickerModeYearMonthDayHourMinute ||
           self.datePickerMode == DatePickerModeTwoYearMonth){
            return 1;
        }
    }
    if([type isEqualToString:Day]){
        if(self.datePickerMode == DatePickerModeYearMonthDay ||
           self.datePickerMode == DatePickerModeYearMonthDayHour ||
           self.datePickerMode == DatePickerModeYearMonthDayHourMinute){
            return 2;
        }
    }
    if([type isEqualToString:Hour]){
        if(self.datePickerMode == DatePickerModeYearMonthDayHour ||
           self.datePickerMode == DatePickerModeYearMonthDayHourMinute){
            return 3;
        }
    }
    if([type isEqualToString:Minute]){
        if(self.datePickerMode == DatePickerModeYearMonthDayHourMinute){
            return 4;
        }
    }
    if([type isEqualToString:Hour]){
        if(self.datePickerMode == DatePickerModeHourMinute24){
            return 0;
        }
    }
    if([type isEqualToString:Minute]){
        if(self.datePickerMode == DatePickerModeHourMinute24){
            return 1;
        }
    }
    if([type isEqualToString:PickerDivisionTip]){
        if(self.datePickerMode == DatePickerModeTwoYearMonth){
            return 2;
        }
    }
    if([type isEqualToString:Year1]){
        if(self.datePickerMode == DatePickerModeTwoYearMonth){
            return 3;
        }
    }
    if([type isEqualToString:Month1]){
        if(self.datePickerMode == DatePickerModeTwoYearMonth){
            return 4;
        }
    }
    return -1;
}

- (NSString*)getPickerViewContent {
    NSString *string = @"";
    for(NSInteger i = 0; i < [self.datePickerView numberOfComponents]; i++){
        NSInteger row = [self.datePickerView selectedRowInComponent:i];
        UILabel *label = (UILabel*)[self.datePickerView viewForRow:row forComponent:i];
        string = [string stringByAppendingString:label.text];
    }
    return string;
}

- (void)setEarliestDate:(NSDate *)earliestDate {
    _earliestDate = earliestDate;
    self.earliestDateComponents = [_calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:earliestDate];
}

- (void)setLatestDate:(NSDate *)latestDate {
    _latestDate = latestDate;
    self.latestDateComponents = [_calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:latestDate];
}

#pragma mark - action

- (void)show {
    [self updateDisplay];
    if(self.showMode == ShowInWindow){
        UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
        [rootVC.view addSubview:self.bgView];
        [rootVC.view addSubview:self];
    }
}

- (void)hide {
    if(self.showMode == ShowInWindow){
        [self.bgView removeFromSuperview];
        [self removeFromSuperview];
    }
}

#pragma mark - Property

- (UIPickerView *)datePickerView {
    if(!_datePickerView){
        _datePickerView = [[UIPickerView alloc] init];
        //以下这行用于iOS8。iOS8下似乎只有三个有效高度，那么高度没法改变，就改下位置好了
        //        _datePickerView.transform = CGAffineTransformMakeTranslation (0, -(_datePickerView.frame.size.height - self.frame.size.height) / 2);
        
        _datePickerView.dataSource = self;
        _datePickerView.delegate = self;
    }
    return _datePickerView;
}

- (UIView *)bgView {
    if(!_bgView){
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height - self.frame.size.height)];
        _bgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    }
    return _bgView;
}

- (ZOEPickerHeaderView*)headerView {
    if(!_headerView){
        _headerView = [[ZOEPickerHeaderView alloc] init];
        _headerView.delegate = self;
    }
    return _headerView;
}

@end

